﻿package mortal.game.view.palyer.view
{
    import Message.DB.Tables.*;
    import Message.Game.*;
    import Message.Public.*;
    import baseEngine.modifiers.*;
    import com.greensock.*;
    import com.greensock.easing.*;
    import fl.data.*;
    import flash.events.*;
    import flash.utils.*;
    import frEngine.*;
    import frEngine.effectEditTool.temple.*;
    import frEngine.pools.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.scene3D.model.player.*;
    import mortal.game.scene3D.model.pools.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.util.*;
    import mortal.game.view.palyer.data.*;
    import mortal.game.view.palyer.util.*;
    import mortal.mvc.core.*;

    public class MonsterSoulPanel extends FashionPanelBase
    {
        private var _tween1:TweenMax;
        private var _tween2:TweenMax;
        private var _modelX:Number;

        public function MonsterSoulPanel(param1:Window)
        {
            _fashionType = EAdvanceComponentType._EAdvanceComponentTypeMonsterSoul;
            super(param1);
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            return;
        }// end function

        override protected function createChildren() : void
        {
            super.createChildren();
            _activeBtn = UIFactory.gLoadingButton(ResFileConst.FightSoulActiveBtn, 299 + 5 - 3 + 265 + 8, 458 - 58 + 108, 114, 33, _window.contentTopOf3DSprite);
            _activeBtn.configEventListener(MouseEvent.CLICK, onActiveHandler);
            _currCombatTxt.imgUrl = ImagesConst.FashionMonsterSoulTxt2;
            _currAttrTxt.imgUrl = ImagesConst.FashionMonsterSoulTxt1;
            _advanceTitle.imgUrl = ImagesConst.FashionMonsterSoulTxt3;
            _skillLabel.imgUrl = ImagesConst.FashionMonsterSoulTxt4;
            _modelTurnLeftBtn.y = 406 - 10;
            _modelTurnRightBtn.y = 406 - 10;
            _rotate3d.addListeners();
            if (FashionUtil.isNeedActiveFashion(_fashionType))
            {
                initBtnState();
            }
            return;
        }// end function

        override protected function initCurrSelCode() : void
        {
            if (_data && _data.advanceInfo)
            {
                updateCurrShap();
            }
            return;
        }// end function

        override protected function initRoleModel() : void
        {
            if (_rect3d)
            {
                this.updateRoleModel();
            }
            return;
        }// end function

        override protected function updateAll() : void
        {
            _data = Cache.instance.fashion.getFashionInfo(_fashionType);
            if (_data && _data.advanceInfo)
            {
                this.updateEquip();
                updateSkill();
                updateLevelName();
                this.updateAttr();
                this.updateCombat();
                this.updatePropItem();
                updateCurrShap();
                updateFashionName();
                clearBtnState();
                updateSwitchBtnState();
                this.updateChangeBtnState();
                updateAdvanceInfo();
                updateProgressInfo();
                updateConsumeInfo();
                updateCd();
            }
            return;
        }// end function

        override protected function updateRoleModel(param1:int = 0) : void
        {
            var _loc_5:TModel = null;
            if (_data == null || _rect3d == null)
            {
                return;
            }
            removeFightSoulModelPlayer();
            removeFightSoulEffectPlayer();
            var _loc_2:* = _currSelCode;
            var _loc_3:* = _data.advanceInfo.state < 0 ? (false) : (true);
            if (!_loc_3)
            {
                _loc_2 = 1;
            }
            var _loc_4:* = MountConfig.instance.getFashionInfoByCode(_fashionType, _loc_2);
            if (_loc_4)
            {
                _loc_5 = ModelConfig.instance.getInfoByCode(_loc_4.modelId);
                if (_loc_5)
                {
                    if (_fightSoulModelPlayer == null || _fightSoulModelPlayer.user != this)
                    {
                        _fightSoulModelPlayer = FrObjectPool.getObject(ActionPlayer, this);
                        if (_loc_3)
                        {
                            _fightSoulModelPlayer.material.materialParams.coloredScreen = false;
                        }
                        else
                        {
                            _fightSoulModelPlayer.material.materialParams.coloredScreen = true;
                        }
                        _rect3d.addObject3d(_fightSoulModelPlayer, 132 + 20 + 90, 280 + 60 + 20);
                        this._modelX = _fightSoulModelPlayer.x;
                        _rotate3d.model = _fightSoulModelPlayer;
                    }
                    _fightSoulModelPlayer.timerContorler = TimeControler.defaultTimeControler;
                    _fightSoulModelPlayer.load(_loc_5.mesh1 + ".md5mesh", _loc_5.bone1 + ".skeleton", _loc_5.texture1, _rect3d.renderList);
                    _fightSoulModelPlayer.play();
                    _fightSoulEffectPlayer = EffectPlayerPool.instance.getEffectPlayer(_loc_5.effect1, null, _rect3d.renderList, false);
                    _fightSoulEffectPlayer.play(PlayMode.ANIMATION_LOOP_MODE);
                    if (_fightSoulEffectPlayer.temple is TempleRole)
                    {
                        TempleRole(_fightSoulEffectPlayer.temple).setRoleParams(_fightSoulModelPlayer, null, null);
                    }
                    else
                    {
                        _fightSoulModelPlayer.addChild(_fightSoulEffectPlayer);
                    }
                    _fightSoulEffectPlayer.resetTimerContorler(_fightSoulModelPlayer.timerContorler);
                }
            }
            return;
        }// end function

        private function updateNextModel() : void
        {
            var _loc_3:TModel = null;
            if (_data == null || _rect3d == null)
            {
                return;
            }
            removeNextFightSoulModelPlayer();
            removeNextFightSoulEffectPlayer();
            var _loc_1:* = _data.advanceInfo.code + 1;
            var _loc_2:* = MountConfig.instance.getFashionInfoByCode(_fashionType, _loc_1);
            if (_loc_2)
            {
                _loc_3 = ModelConfig.instance.getInfoByCode(_loc_2.modelId);
                if (_loc_3)
                {
                    if (_nextFightSoulModelPlayer == null || _nextFightSoulModelPlayer.user != this)
                    {
                        _nextFightSoulModelPlayer = FrObjectPool.getObject(ActionPlayer, this);
                        _rect3d.addObject3d(_nextFightSoulModelPlayer, 132 + 20, 280 + 60 + 20);
                        _nextFightSoulModelPlayer.rotationY = 50;
                    }
                    _nextFightSoulModelPlayer.load(_loc_3.mesh1 + ".md5mesh", _loc_3.bone1 + ".skeleton", _loc_3.texture1, _rect3d.renderList);
                    _nextFightSoulModelPlayer.play();
                    _nextFightSoulEffectPlayer = EffectPlayerPool.instance.getEffectPlayer(_loc_3.effect1, null, _rect3d.renderList, false);
                    _nextFightSoulEffectPlayer.play(PlayMode.ANIMATION_LOOP_MODE);
                    if (_nextFightSoulEffectPlayer.temple is TempleRole)
                    {
                        TempleRole(_nextFightSoulEffectPlayer.temple).setRoleParams(_nextFightSoulModelPlayer, null, null);
                    }
                    else
                    {
                        _nextFightSoulModelPlayer.addChild(_nextFightSoulEffectPlayer);
                    }
                    _nextFightSoulEffectPlayer.resetTimerContorler(_nextFightSoulModelPlayer.timerContorler);
                    _fightSoulModelPlayer.rotationY = 50;
                    _fightSoulModelPlayer.rotationY = _fightSoulModelPlayer.rotationY % 360;
                    this._tween1 = TweenMax.to(_fightSoulModelPlayer, 1, {x:this._modelX - 105, rotationY:-50, ease:Quint.easeOut});
                    this._tween2 = TweenMax.to(_nextFightSoulModelPlayer, 1, {x:this._modelX + 105, alpha:0.6, ease:Quint.easeOut});
                }
            }
            return;
        }// end function

        override protected function updateEquip() : void
        {
            var _loc_3:ItemData = null;
            var _loc_4:ItemEquipInfo = null;
            var _loc_6:int = 0;
            var _loc_7:Object = null;
            var _loc_1:* = Cache.instance.pack.getFashionPackCacheByCode(EAdvanceComponentType._EAdvanceComponentTypeMonsterSoul).getAllEquip();
            var _loc_2:* = new Array(4);
            for each (_loc_3 in _loc_1)
            {
                
                if (_loc_3)
                {
                    _loc_2[_loc_3.itemInfo.type] = _loc_3;
                }
            }
            if (_loc_1 && _loc_1.length < 4)
            {
                _loc_6 = 0;
                while (_loc_6 < _loc_2.length)
                {
                    
                    if (!_loc_2[_loc_6])
                    {
                        _loc_4 = ItemConfig.instance.getFashionEquipByLevel(EProp._EPropFashionMonsterSoulEquip, _loc_6, 3, 3);
                        if (_loc_4)
                        {
                            _loc_2[_loc_6] = new ItemData(_loc_4.code);
                            _loc_7 = {};
                            _loc_7.isDefault = true;
                            _loc_2[_loc_6].extData = _loc_7;
                        }
                    }
                    _loc_6++;
                }
            }
            var _loc_5:* = new DataProvider();
            _loc_5.addItems(_loc_2);
            _equipList.dataProvider = _loc_5;
            _equipList.drawNow();
            return;
        }// end function

        override protected function updateCombat() : void
        {
            var _loc_2:AdvanceAttrData = null;
            var _loc_3:int = 0;
            var _loc_4:Number = NaN;
            var _loc_1:Number = 0;
            for each (_loc_2 in _currAdvanceAttrData)
            {
                
                _loc_1 = _loc_1 + CombatUtil.getCombatValueByName(_loc_2.attrName, _loc_2.totalValue);
            }
            _loc_3 = FashionUtil.getSkillCombat(_fashionType);
            _combat.text = int(_loc_1 + _loc_3) + "";
            _loc_4 = 0;
            for each (_loc_2 in _currAdvanceAttrData)
            {
                
                _loc_4 = _loc_4 + CombatUtil.getCombatValueByName(_loc_2.attrName, _loc_2.attrUpValue);
            }
            _upCombat.text = int(_loc_4) + "";
            return;
        }// end function

        override protected function updateAttr() : void
        {
            var _loc_1:TMountConfig = null;
            var _loc_2:TMountConfig = null;
            var _loc_3:Array = null;
            var _loc_4:Array = null;
            var _loc_5:String = null;
            var _loc_6:DataProvider = null;
            var _loc_7:AdvanceAttrData = null;
            var _loc_8:AdvanceAttrData = null;
            if (_data.advanceInfo)
            {
                _loc_1 = MountConfig.instance.getFashionInfoByCode(_fashionType, _data.advanceInfo.code);
                _loc_2 = MountConfig.instance.getFashionInfoByCode(_fashionType, (_data.advanceInfo.code + 1));
                if (_loc_1)
                {
                    _loc_3 = [];
                    _loc_4 = FashionUtil.attrs;
                    for each (_loc_5 in _loc_4)
                    {
                        
                        _loc_7 = this.getAdvanceAttrData(_loc_5, _loc_1);
                        if (_loc_2)
                        {
                            _loc_8 = this.getAdvanceAttrData(_loc_5, _loc_2);
                            _loc_7.attrUpValue = _loc_8.totalValue - _loc_7.totalValue;
                        }
                        _loc_3.push(_loc_7);
                    }
                    _currAdvanceAttrData = _loc_3;
                    _loc_6 = new DataProvider();
                    _loc_6.addItems(_loc_3);
                    _attrList.dataProvider = _loc_6;
                    _attrList.drawNow();
                }
            }
            return;
        }// end function

        private function getAdvanceAttrData(param1:String, param2:TMountConfig) : AdvanceAttrData
        {
            var _loc_3:* = new AdvanceAttrData();
            _loc_3.attrName = param1;
            _loc_3.baseAttrValue = param2[param1] as int;
            var _loc_4:* = new ItemData(FashionConst.PropCode_MonsterSoul_Growth);
            _loc_3.growthAttrValue = FashionUtil.getGrowthGainAttr(_loc_4, param1, _data.advanceInfo.totalGrowthNum);
            var _loc_5:* = new ItemData(FashionConst.PropCode_MonsterSoul_Talent);
            var _loc_6:* = _loc_5.itemInfo.effectEx / 100;
            _loc_3.talentAttrValue = _loc_3.baseAttrValue * _loc_6 * _data.advanceInfo.totalTalentNum;
            var _loc_7:* = this.getEquipGainAttr();
            _loc_3.equipAttrValue = _loc_7[param1] as int;
            return _loc_3;
        }// end function

        private function getEquipGainAttr() : Dictionary
        {
            var _loc_4:String = null;
            var _loc_5:ItemData = null;
            var _loc_1:* = Cache.instance.pack.getFashionPackCacheByCode(EAdvanceComponentType._EAdvanceComponentTypeMonsterSoul).getAllEquip();
            var _loc_2:* = new Dictionary();
            var _loc_3:* = AttributeUtil.attrs.concat();
            _loc_3.splice(2, 1);
            _loc_3.splice(-1);
            for each (_loc_4 in _loc_3)
            {
                
                _loc_2[_loc_4] = 0;
            }
            for each (_loc_5 in _loc_1)
            {
                
                for each (_loc_4 in _loc_3)
                {
                    
                    _loc_2[_loc_4] = _loc_2[_loc_4] + (_loc_5.itemInfo as ItemEquipInfo)[_loc_4];
                }
            }
            return _loc_2;
        }// end function

        private function onHideClothHandler(event:MouseEvent) : void
        {
            GameProxy.fashion.hideOrShowFashion(EFashionType._EFashionFightSoul);
            return;
        }// end function

        override protected function onMonsterSoulEquipUpdate(param1:Object) : void
        {
            this.updateEquip();
            this.updateAttr();
            this.updateCombat();
            return;
        }// end function

        override protected function updatePropItem() : void
        {
            if (_data.advanceInfo.code >= 4)
            {
                _growthItem.isGray = false;
                _growthOpenTip.visible = false;
            }
            if (_data.advanceInfo.code >= 5)
            {
                _talentItem.isGray = false;
                _talentOpenTip.visible = false;
            }
            this.updatePropAmount();
            return;
        }// end function

        override protected function updatePropAmount() : void
        {
            _growthItem.itemData = new ItemData(FashionConst.PropCode_MonsterSoul_Growth);
            var _loc_1:* = Cache.instance.pack.backPackCache.getItemCountByItemData(_growthItem.itemData);
            _growthItem.amount = _loc_1;
            if (_loc_1 > 0)
            {
                _growthItem.setAmountLabel(_loc_1 + "");
                if (_data.advanceInfo.code >= 4)
                {
                    _fashionGrowthUseBtn.visible = true;
                    EffectManager.glowFilterReg(_growthItem);
                }
            }
            else
            {
                _fashionGrowthUseBtn.visible = false;
                EffectManager.glowFilterUnReg(_growthItem);
            }
            _talentItem.itemData = new ItemData(FashionConst.PropCode_MonsterSoul_Talent);
            var _loc_2:* = Cache.instance.pack.backPackCache.getItemCountByItemData(_talentItem.itemData);
            _talentItem.amount = _loc_2;
            if (_loc_2 > 0)
            {
                _talentItem.setAmountLabel(_loc_2 + "");
                if (_data.advanceInfo.code >= 5)
                {
                    _fashionTalentUseBtn.visible = true;
                    EffectManager.glowFilterReg(_talentItem);
                }
            }
            else
            {
                _fashionTalentUseBtn.visible = false;
                EffectManager.glowFilterUnReg(_talentItem);
            }
            return;
        }// end function

        override protected function useItemHandler(event:MouseEvent) : void
        {
            var _loc_2:ItemData = null;
            var _loc_3:Array = null;
            if (event.target == _fashionGrowthUseBtn)
            {
                if (_growthItem.amount > 0)
                {
                    _loc_3 = Cache.instance.pack.backPackCache.getItemsByGroupCategoryAndType(EGroup._EGroupStuff, EStuff._EStuffAdvance, EAdvanceType._EAdvanceTypeFashionMonsterSoulGrowth);
                    _loc_2 = _loc_3[0] as ItemData;
                }
                if (_loc_2)
                {
                    GameProxy.packProxy.useItem(_loc_2.uid, _loc_2.itemInfo.code);
                }
            }
            else if (event.target == _fashionTalentUseBtn)
            {
                if (_talentItem.amount > 0)
                {
                    _loc_3 = Cache.instance.pack.backPackCache.getItemsByGroupCategoryAndType(EGroup._EGroupStuff, EStuff._EStuffAdvance, EAdvanceType._EAdvanceTypeFashionMonsterSoulTalent);
                    _loc_2 = _loc_3[0] as ItemData;
                }
                if (_loc_2)
                {
                    GameProxy.packProxy.useItem(_loc_2.uid, _loc_2.itemInfo.code);
                }
            }
            return;
        }// end function

        private function onOverHandler(event:MouseEvent) : void
        {
            var _loc_2:* = _data.advanceInfo.code + 1;
            if (_loc_2 <= FashionConst.FashionAdvanceMaxLevel)
            {
                this.updateRoleModel(_loc_2);
            }
            return;
        }// end function

        private function onOutHandler(event:MouseEvent) : void
        {
            this.updateRoleModel();
            return;
        }// end function

        override protected function onAttrOverHandler(event:MouseEvent = null) : void
        {
            var _loc_3:TMountConfig = null;
            var _loc_2:* = _data.advanceInfo.code + 1;
            if (_loc_2 <= FashionConst.FashionAdvanceMaxLevel)
            {
                super.onAttrOverHandler(event);
                _levelUpArrow.visible = true;
                this.skillTween();
                this.updateNextModel();
                _beforeAdvance.visible = true;
                _afterAdvance.visible = true;
                _currLevelName.visible = true;
                _currLevelName.text = _data.baseInfo.name;
                _loc_3 = MountConfig.instance.getFashionInfoByCode(_fashionType, _loc_2);
                if (_loc_3)
                {
                    _nextLevelName.visible = true;
                    _nextLevelName.text = _loc_3.name;
                }
                _upCombat.visible = true;
                _upCombatArrow.visible = true;
            }
            return;
        }// end function

        override protected function onAttrOutHandler(event:MouseEvent = null) : void
        {
            var _loc_2:* = _data.advanceInfo.code + 1;
            if (_loc_2 <= FashionConst.FashionAdvanceMaxLevel)
            {
                super.onAttrOutHandler(event);
                _levelUpArrow.visible = false;
                this.skillTween();
                if (_fightSoulModelPlayer)
                {
                    this._tween1 = TweenMax.to(_fightSoulModelPlayer, 0.6, {x:this._modelX, rotationY:0, ease:Quint.easeOut});
                }
                if (_nextFightSoulEffectPlayer)
                {
                    if (_rect3d)
                    {
                        _rect3d.disposeObj3d(_nextFightSoulEffectPlayer);
                    }
                    else
                    {
                        _nextFightSoulEffectPlayer.dispose();
                    }
                    _nextFightSoulEffectPlayer = null;
                }
                if (_nextFightSoulModelPlayer)
                {
                    this._tween2 = TweenMax.to(_nextFightSoulModelPlayer, 0.6, {x:this._modelX, alpha:0.05, ease:Quint.easeOut, onComplete:this.onShowEnd});
                }
                _beforeAdvance.visible = false;
                _afterAdvance.visible = false;
                _currLevelName.visible = false;
                _nextLevelName.visible = false;
                _upCombat.visible = false;
                _upCombatArrow.visible = false;
            }
            return;
        }// end function

        private function skillTween() : void
        {
            if (this._tween1 && this._tween2)
            {
                this._tween1.kill();
                this._tween2.kill();
                this._tween1 = null;
                this._tween2 = null;
            }
            return;
        }// end function

        private function onShowEnd() : void
        {
            if (_rect3d)
            {
                if (_nextFightSoulModelPlayer)
                {
                    _rect3d.disposeObj3d(_nextFightSoulModelPlayer);
                    _nextFightSoulModelPlayer = null;
                }
                if (_nextFightSoulEffectPlayer)
                {
                    _rect3d.disposeObj3d(_nextFightSoulEffectPlayer);
                    _nextFightSoulEffectPlayer = null;
                }
            }
            return;
        }// end function

        override protected function onWindowDragEnd(event:Event) : void
        {
            if (_rect3d && _fightSoulModelPlayer)
            {
                _rect3d.disposeObj3d(_fightSoulModelPlayer);
                _fightSoulModelPlayer = null;
                _rect3d.disposeObj3d(_fightSoulEffectPlayer);
                _fightSoulEffectPlayer = null;
                this.initRoleModel();
            }
            return;
        }// end function

        override protected function removeAllModel() : void
        {
            if (_rect3d)
            {
                if (_fightSoulModelPlayer)
                {
                    _rect3d.disposeObj3d(_fightSoulModelPlayer);
                    _fightSoulModelPlayer = null;
                }
                if (_fightSoulEffectPlayer)
                {
                    _rect3d.disposeObj3d(_fightSoulEffectPlayer);
                    _fightSoulEffectPlayer = null;
                }
            }
            return;
        }// end function

        override protected function onAdvanceSucc() : void
        {
            var _loc_3:TMountConfig = null;
            var _loc_4:TMountConfig = null;
            var _loc_1:* = _data.advanceInfo.code;
            var _loc_2:* = _data.advanceInfo.code + 1;
            if (_loc_2 <= FashionConst.FashionAdvanceMaxLevel)
            {
                _loc_3 = MountConfig.instance.getFashionInfoByCode(_fashionType, _loc_1);
                _loc_4 = MountConfig.instance.getFashionInfoByCode(_fashionType, _loc_2);
                if (_loc_3 && _loc_4)
                {
                    _currLevelName.text = _loc_3.name;
                    _nextLevelName.text = _loc_4.name;
                }
            }
            if (_rect3d && _nextFightSoulModelPlayer)
            {
                _rect3d.disposeObj3d(_nextFightSoulModelPlayer);
                _nextFightSoulModelPlayer = null;
                if (_nextFightSoulEffectPlayer)
                {
                    _rect3d.disposeObj3d(_nextFightSoulEffectPlayer);
                    _nextFightSoulEffectPlayer = null;
                }
                this.updateNextModel();
            }
            return;
        }// end function

        override protected function onClickTurnBtn(event:MouseEvent) : void
        {
            if (event.target == _modelTurnLeftBtn)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.FashionModelTurnLeft));
            }
            else if (event.target == _modelTurnRightBtn)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.FashionModelTurnRight));
            }
            return;
        }// end function

        override protected function stopTurning(event:MouseEvent = null) : void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.FashionModelStopTurning));
            return;
        }// end function

        override protected function updateChangeBtnState() : void
        {
            _sharpBtn.visible = false;
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            _activeBtn.dispose();
            _activeBtn = null;
            this.skillTween();
            this._modelX = 0;
            return;
        }// end function

    }
}
